package org.activiti.engine.impl.cfg;

import org.activiti.engine.impl.interceptor.Command;
import org.activiti.engine.impl.interceptor.CommandConfig;
import org.activiti.engine.impl.interceptor.CommandExecutor;
import org.activiti.engine.impl.interceptor.CommandInterceptor;

/**
 * Command executor that passes commands to the first interceptor in the chain. If no {@link CommandConfig} is passed, the default configuration will be used.
 * 命令执行者 第一个 执行环节
 * <p>
 * repositoryService,runtimeService,historyService,taskService 中的 CommandExecutor
 * 都是这个实现单例
 */
public class CommandExecutorImpl implements CommandExecutor {

    /**
     * 命令 配置器
     */
    protected CommandConfig defaultConfig;
    /**
     * 命令执行器拦截器链，
     * 拦截器链 对象值 顺序如下
     *
     * @see org.activiti.engine.impl.interceptor.LogInterceptor
     * SpringTransactionInterceptor
     * @see org.activiti.engine.impl.interceptor.CommandContextInterceptor
     * @see org.activiti.engine.impl.interceptor.TransactionContextInterceptor
     * @see org.activiti.engine.impl.interceptor.CommandInvoker
     */
    protected CommandInterceptor first;

    public CommandExecutorImpl(CommandConfig defaultConfig, CommandInterceptor first) {
        this.defaultConfig = defaultConfig;
        this.first = first;
    }

    public CommandInterceptor getFirst() {
        return first;
    }

    public void setFirst(CommandInterceptor commandInterceptor) {
        this.first = commandInterceptor;
    }

    @Override
    public CommandConfig getDefaultConfig() {
        return defaultConfig;
    }

    @Override
    public <T> T execute(Command<T> command) {
        return execute(defaultConfig, command);
    }

    @Override
    public <T> T execute(CommandConfig config, Command<T> command) {
        return first.execute(config, command);
    }

}
